<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>payMent | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="微信公众号h5页面支付前端流程 123456789101112131415161718192021222324252627mounted() &amp;#123;	let openId &#x3D; uni.getStorageSync(&amp;#x27;fy_openid&amp;#x27;);	if (openId) &amp;#123;		this.openId &#x3D; openId;		return true	&amp;#125;	let">
<meta property="og:type" content="article">
<meta property="og:title" content="payMent">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2023/06/29/payMent/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="微信公众号h5页面支付前端流程 123456789101112131415161718192021222324252627mounted() &amp;#123;	let openId &#x3D; uni.getStorageSync(&amp;#x27;fy_openid&amp;#x27;);	if (openId) &amp;#123;		this.openId &#x3D; openId;		return true	&amp;#125;	let">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-06-29T14:12:00.000Z">
<meta property="article:modified_time" content="2023-06-20T08:20:04.513Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-payMent" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2023/06/29/payMent/" class="article-date">
  <time class="dt-published" datetime="2023-06-29T14:12:00.000Z" itemprop="datePublished">2023-06-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      payMent
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>微信公众号h5页面支付前端流程</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">mounted</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">let</span> openId = uni.<span class="title function_">getStorageSync</span>(<span class="string">&#x27;fy_openid&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (openId) &#123;</span><br><span class="line">		<span class="variable language_">this</span>.<span class="property">openId</span> = openId;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">let</span> code = <span class="variable language_">this</span>.<span class="title function_">getUrlKey</span>(<span class="string">&#x27;code&#x27;</span>);</span><br><span class="line">	<span class="keyword">if</span> (!code) &#123;</span><br><span class="line">		<span class="keyword">let</span> url =</span><br><span class="line">			<span class="string">&quot;https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5e5d798832f4d853&amp;redirect_uri=&quot;</span> +</span><br><span class="line">			<span class="built_in">encodeURIComponent</span>(location.<span class="property">href</span>) + <span class="string">&quot;&amp;response_type=code&amp;scope=snsapi_base&amp;state=123#wechat_redirect&quot;</span>;</span><br><span class="line">		location.<span class="property">href</span> = url;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="title function_">getOpenIdBycode</span>(code).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">			<span class="keyword">if</span> (res.<span class="property">code</span> == <span class="number">0</span>) &#123;</span><br><span class="line">				<span class="variable language_">this</span>.<span class="property">openId</span> = res.<span class="property">data</span>.<span class="property">openId</span>;</span><br><span class="line">				uni.<span class="title function_">setStorageSync</span>(<span class="string">&#x27;fy_openid&#x27;</span>, res.<span class="property">data</span>.<span class="property">openId</span>);</span><br><span class="line">			&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">				uni.<span class="title function_">showToast</span>(&#123;</span><br><span class="line">					<span class="attr">icon</span>: <span class="string">&#x27;error&#x27;</span>,</span><br><span class="line">					<span class="attr">title</span>: res.<span class="property">msg</span> || <span class="string">&quot;网络出现异常，请重新扫码后进入&quot;</span>,</span><br><span class="line">					<span class="attr">duration</span>: <span class="number">2000</span></span><br><span class="line">				&#125;)</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">getUrlKey</span>(<span class="params">name</span>) &#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">decodeURIComponent</span>((<span class="keyword">new</span> <span class="title class_">RegExp</span>(<span class="string">&#x27;[?|&amp;]&#x27;</span> + name + <span class="string">&#x27;=&#x27;</span> + <span class="string">&#x27;([^&amp;;]+?)(&amp;|#|;|$)&#x27;</span>).<span class="title function_">exec</span>(location.<span class="property">href</span>) || [, <span class="string">&quot;&quot;</span>])[<span class="number">1</span>].<span class="title function_">replace</span>(<span class="regexp">/\+/g</span>, <span class="string">&#x27;%20&#x27;</span>)) || <span class="literal">null</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">placeOrder</span>(&#123;</span><br><span class="line">	<span class="attr">money</span>: sendMoney,</span><br><span class="line">	<span class="attr">openId</span>: <span class="variable language_">this</span>.<span class="property">openId</span>,</span><br><span class="line">	<span class="attr">memberCard</span>: <span class="variable language_">this</span>.<span class="property">id</span>,</span><br><span class="line">	<span class="attr">payType</span>: <span class="number">1</span></span><br><span class="line">&#125;).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> (res.<span class="property">code</span> == <span class="number">0</span>) &#123;</span><br><span class="line">			wx.<span class="title function_">hideLoading</span>();</span><br><span class="line">			<span class="keyword">return</span> <span class="title function_">toPay</span>(res.<span class="property">data</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		uni.<span class="title function_">showToast</span>(&#123;</span><br><span class="line">			<span class="attr">icon</span>: <span class="string">&#x27;none&#x27;</span>,</span><br><span class="line">			<span class="attr">title</span>: res.<span class="property">msg</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">		uni.<span class="title function_">showToast</span>(&#123;</span><br><span class="line">			<span class="attr">icon</span>: <span class="string">&#x27;none&#x27;</span>,</span><br><span class="line">			<span class="attr">title</span>: err.<span class="property">data</span> || <span class="string">&quot;错误&quot;</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">toPay</span>(<span class="params">data</span>) &#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">			<span class="title class_">WexinPayFactory</span>.<span class="title function_">wexinPay</span>(<span class="title class_">JSON</span>.<span class="title function_">parse</span>(data.<span class="property">body</span>), <span class="keyword">function</span>(<span class="params">res</span>) &#123;</span><br><span class="line">				uni.<span class="title function_">showToast</span>(&#123;</span><br><span class="line">					<span class="attr">title</span>: <span class="string">&quot;支付成功&quot;</span>,</span><br><span class="line">					<span class="attr">duration</span>: <span class="number">2000</span></span><br><span class="line">				&#125;);</span><br><span class="line">			&#125;);</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	微信支付方法(uni-app h5)适用</span></span><br><span class="line"><span class="comment">	获取微信加签信息</span></span><br><span class="line"><span class="comment">	@param&#123;data&#125;:获取的微信加签</span></span><br><span class="line"><span class="comment">	@param&#123;res&#125;:成功回调</span></span><br><span class="line"><span class="comment">	@param&#123;fail&#125;:失败回调</span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">	@warn:因为package为严格模式下的保留字，不能用作变量.</span></span><br><span class="line"><span class="comment">	@use</span></span><br><span class="line"><span class="comment">	</span></span><br><span class="line"><span class="comment">		wPay(&#123;</span></span><br><span class="line"><span class="comment">			appId,</span></span><br><span class="line"><span class="comment">			timeStamp,</span></span><br><span class="line"><span class="comment">			nonceStr,</span></span><br><span class="line"><span class="comment">			signature,</span></span><br><span class="line"><span class="comment">			package,</span></span><br><span class="line"><span class="comment">			paySign</span></span><br><span class="line"><span class="comment">		&#125;,res=&gt;&#123;</span></span><br><span class="line"><span class="comment">			console.log(‘调用成功!‘);</span></span><br><span class="line"><span class="comment">		&#125;,fail=&gt;&#123;</span></span><br><span class="line"><span class="comment">			console.log(‘调用失败!‘);</span></span><br><span class="line"><span class="comment">		&#125;)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">const</span> wx = <span class="built_in">require</span>(<span class="string">&#x27;jweixin-module&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">WexinPayFactory</span> &#123;</span><br><span class="line">	<span class="title function_">wexinPay</span>(<span class="params">data, successCallBack</span>) &#123;</span><br><span class="line">		<span class="keyword">let</span> [appId, timestamp, nonceStr, signature, packages, paySign] = [data.<span class="property">appId</span>, data.<span class="property">timeStamp</span>, data.<span class="property">nonceStr</span>, data.<span class="property">sign</span>,</span><br><span class="line">			data.<span class="property">package</span>, data.<span class="property">sign</span></span><br><span class="line">		];</span><br><span class="line">		wx.<span class="title function_">config</span>(&#123;</span><br><span class="line">			<span class="attr">debug</span>: <span class="literal">false</span>, <span class="comment">// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。</span></span><br><span class="line">			appId, <span class="comment">// 必填，公众号的唯一标识</span></span><br><span class="line">			timestamp, <span class="comment">// 必填，生成签名的时间戳</span></span><br><span class="line">			nonceStr, <span class="comment">// 必填，生成签名的随机串</span></span><br><span class="line">			signature, <span class="comment">// 必填，签名，见附录1</span></span><br><span class="line">			<span class="attr">jsApiList</span>: [<span class="string">&#x27;chooseWXPay&#x27;</span>] <span class="comment">// 必填，需要使用的JS接口列表，所有JS接口列表见附录2</span></span><br><span class="line">		&#125;);</span><br><span class="line">		wx.<span class="title function_">ready</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">			wx.<span class="title function_">chooseWXPay</span>(&#123;</span><br><span class="line">				timestamp, <span class="comment">// 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符</span></span><br><span class="line">				nonceStr, <span class="comment">// 支付签名随机串，不长于 32 位</span></span><br><span class="line">				<span class="string">&#x27;package&#x27;</span>: packages, <span class="comment">// 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）</span></span><br><span class="line">				<span class="attr">signType</span>: <span class="string">&#x27;MD5&#x27;</span>, <span class="comment">// 签名方式，默认为‘SHA1‘，使用新版支付需传入‘MD5‘</span></span><br><span class="line">				paySign, <span class="comment">// 支付签名</span></span><br><span class="line">				<span class="title function_">success</span>(<span class="params">res</span>) &#123;</span><br><span class="line">					<span class="comment">// 支付成功后的回调函数</span></span><br><span class="line">					<span class="title function_">successCallBack</span>(res);</span><br><span class="line">				&#125;,</span><br><span class="line">				<span class="title function_">fail</span>(<span class="params">res</span>) &#123;</span><br><span class="line">					uni.<span class="title function_">showToast</span>(&#123;</span><br><span class="line">						<span class="attr">icon</span>: <span class="string">&#x27;none&#x27;</span>,</span><br><span class="line">						<span class="attr">title</span>: <span class="string">&#x27;支付失败&#x27;</span></span><br><span class="line">					&#125;)</span><br><span class="line"></span><br><span class="line">					<span class="keyword">return</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">		&#125;);</span><br><span class="line"></span><br><span class="line">		wx.<span class="title function_">error</span>(<span class="keyword">function</span>(<span class="params">res</span>) &#123;</span><br><span class="line">			<span class="comment">// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。</span></span><br><span class="line">			<span class="comment">/*alert(&quot;config信息验证失败&quot;);*/</span></span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = <span class="keyword">new</span> <span class="title class_">WexinPayFactory</span>();</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2023/06/29/payMent/" data-id="cljmgj1ey000nzc9j7klvenk3" data-title="payMent" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          工作日志
        
      </div>
    </a>
  
  
    <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">原理题</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>